/**
 * Program  : MainService.java
 * Author   : qianj
 * Create   : 2012-6-11 下午3:40:11
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package cn.learn.base.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.learn.base.IBaseActivity;
import cn.learn.base.util.DebugUtil;


import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

/**
 * 
 * @author   qianj
 * @version  1.0.0
 * @2012-6-11 下午3:40:11
 */
public class MainService extends Service implements Runnable{
	
	
	public boolean threadDisable; // 线程运行标志
	public static List<Task> allTask = new ArrayList<Task>();//任务池
	public static List<Activity> allActivity = new ArrayList<Activity>();
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		Thread t = new Thread(this);
		t.start();

		DebugUtil.debug("main.service", "main service on create");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		threadDisable = true; // 设置线程运行标志为true来停止线程
		DebugUtil.debug("main.service", "main service shutdown.");
	}

	//添加任务
	public static void addTask(Task task) {
		allTask.add(task);
	}

	public void doTask(Task task) {
		System.out.println("***----doTask----");
		Message mess = handler.obtainMessage();
		mess.what = task.getTaskId();
		try {
			switch (mess.what) {
			case Task.TASK_LOGIN:
				//登陆代码
				
				//登陆成功给obj赋值
				mess.obj = new Object();
				break;
			case Task.TASK_GET_CITIES:
				//List<City> cities = myapp.getCities();
				//System.out.println("all cities = " + cities);
				//mess.obj = cities;
				break;
			case Task.TASK_NEW_CITY:
				Map<String, Object> params = task.getParams();
				System.out.println("create a new city,name is "
						+ params.get("name"));
				break;
			}
		} catch (Exception me) {
			me.printStackTrace();
		}

		handler.sendMessage(mess); // 发送消息更新UI
		allTask.remove(task); // 移除任务
	}

	@Override
	public void run() {
		while (!threadDisable) {
			Task lastTask = null;
			if (allTask.size() > 0) {
				lastTask = allTask.get(0);
				doTask(lastTask);
			}
			// 休眠小段时间
			try {
				Thread.sleep(1000);
			} catch (Exception e) {

			}
		}
	}
	
	
	public static Activity getActivityByName(String name) {
		System.out.println("all activity's size:" + allActivity.size());
		int i = allActivity.size() - 1;
		for( ; i>=0;i--) {
			Activity ac = allActivity.get(i);
			System.out.println(ac.toString());
			if (ac.getClass().getName().indexOf(name) > 0) {
				return ac;
			}
		}
		return null;
	}
	

	// 更新UI
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			System.out.println("~~~~Handler~~~~~~~~~");
			IBaseActivity iba = null;
			switch (msg.what) {
			case Task.TASK_GET_CITIES:
				iba = (IBaseActivity) getActivityByName("Cities");
				iba.refreshUI(1, msg.obj);
				break;
			case Task.TASK_NEW_CITY:
				iba = (IBaseActivity) getActivityByName("NewCity");
				iba.refreshUI();
				break;
			case Task.TASK_LOGIN:
				iba = (IBaseActivity) getActivityByName("Login");
				iba.refreshUI(msg.what, msg.obj);
				break;

			}
		}
	};

	public static void exitApp(Context con) {
		System.out.println("exit app...");
		
		//结束所有activity
		System.out.println("all activity's size:" + allActivity.size());
		for (Activity ac : allActivity) {
			System.out.println("ac:" + ac.toString() + ",finish...");
			ac.finish();
		}
		
		//停止后台服务
		Intent it = new Intent("com.lixw.myandroid.MainService");
		con.stopService(it);
		
		//关闭httpclicent资源
		//HttpClient.shutdown();
		
		//退出系统
		System.exit(0);
	}
	
	
	

}

